package com.example.skycode7.servlet;

import com.example.skycode7.bean.AdminBean;
import com.example.skycode7.dao.AdminDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/editAdd.jsp")
public class AdminEditServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String User =(String) req.getSession().getAttribute("User");
        if (User == null || User.equals("")) {
            resp.sendRedirect( "login.jsp");//用户没登陆 跳转到登录页面
            return;//页面显示和重定向只能生效一个
        }
        req.setCharacterEncoding("UTF-8");
        String adminId = req.getParameter("AdminID");
        String adminTitle = req.getParameter("AdminTitle");
        String adminContent = req.getParameter("AdminContent");
        AdminBean adminBean = new AdminBean();//实例化一个新闻对象
        //设置新闻对象属性值
        adminBean.setAdminID(Integer.parseInt(adminId));
        adminBean.setAdminType(1);
        adminBean.setAdminName(adminContent);
        adminBean.setLastLoginTime((new Date()).toString());
        adminBean.setAdminName(User);
        int result =(new AdminDao()).update(adminBean);
        if (result > 0) {
            //补充知识 跳转到网页news.html中
            resp.sendRedirect("news.html");//使用结果对象 完成页面网址跳转
        } else {//失败显示错误提示页面
            req.getRequestDispatcher("WEB-INF/error.jsp").forward(req, resp);
        }
    }
}
